package file;

import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;

public class FileServer extends Thread {
	
	int portNumber;
	
	Socket socket = null;
	
	ServerSocket serverSocket = null;
	FileServerThread fileServerThread = null;

	boolean flag = true;
	
	HashMap<String, FileServerThread> clients = new HashMap<String, FileServerThread>();
	
	public FileServer(int portNumber) {
		
		this.portNumber = portNumber;
		System.out.println("중개서버에서 포트번호를 보내왔습니다.\n포트번호는: "+this.portNumber+"입니다.");
		start();
	}
	
	@Override
	public void run() {
		
		try {
			if(serverSocket != null) serverSocket.close();
			serverSocket = new ServerSocket(portNumber);
			System.out.println("파일서버가 시작되었습니다.");
			
			while(flag){
				socket = serverSocket.accept();
				InetAddress ia = socket.getInetAddress();
				String ip = ia.getHostAddress();
				new FileServerThread(this, socket);
				System.out.println(ip+"의 클라이언트가 파일 서버에 접속하셨습니다.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
